package Easy;

//38. 外观数列
public class Solution38 {
    public String countAndSay(int n) {
        if (n == 1)
            return "1";
        if (n == 2)
            return "11";
        String pre = countAndSay(n - 1);
        int sum_count = 1;
        int count = 1;
        for (int i = 0; i < pre.length() - 1; i++) {
            if (pre.charAt(i) != pre.charAt(i + 1))
                sum_count++;
        }
        char[] post = new char[2 * sum_count];
        sum_count = 0;
        for (int i = 0; i < pre.length() - 1; i++) {
            if (pre.charAt(i) == pre.charAt(i + 1)) {
                count++;
            } else {
                post[sum_count] = (char) (count + 48);
                post[sum_count + 1] = pre.charAt(i);
                count = 1;
                sum_count += 2;
            }
        }
        post[post.length-2] = (char) (count + 48);
        post[post.length-1] = pre.charAt(pre.length()-1);
        String res = new String(post);
        return res;
    }

    public static void main(String[] args) {
        Solution38 s = new Solution38();
        System.out.println(s.countAndSay(6));
    }

}
